package com.bhex.kline.widget.tab;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bhex.kline.KlineKind;
import com.bhex.kline.R;
import com.bhex.kline.widget.TabButtonItem;
import com.bhex.kline.widget.util.PixelUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by BHEX.
 * User: gdy
 * Date: 2020/3/24
 * Time: 14:17
 */
public class KLineTabLayout extends LinearLayout implements View.OnClickListener {

    private CheckTabListener mCheckTabListener;

    private ArrayList<TabButtonItem> mTabList;

    private int mWidth,mHeight;

    private int mItemWidth;

    private int maxCount = 6;

    private Context mContext;

    private List<TabTextView> mChildViews;

    private int mTextNormalColor;

    private int mTextPressColor;

    private int mStrokeWidth;

    private int mStrokeColor;

    private int normalFillColor;

    private int pressFillColor;

    public KLineTabLayout(Context context) {
        this(context,null);
    }

    public KLineTabLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public KLineTabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }


    private void init(Context context, AttributeSet attrs) {

        setOrientation(LinearLayout.HORIZONTAL);

        mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SegmentButton);

        mTextNormalColor = ta.getColor(R.styleable.SegmentButton_textNormalColor, Color.WHITE);
        mTextPressColor = ta.getColor(R.styleable.SegmentButton_textPressColor, Color.WHITE);

        mStrokeColor = ta.getColor(R.styleable.SegmentButton_strokeColor,Color.parseColor("#3375E0"));

        normalFillColor = ta.getColor( R.styleable.SegmentButton_normalFillColor,Color.TRANSPARENT);

        pressFillColor = ta.getColor(R.styleable.SegmentButton_pressFillColor, Color.parseColor("#3375E0"));

        mStrokeWidth = PixelUtils.dp2px(mContext,1.5f);
        mChildViews = new ArrayList<>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        mItemWidth = mWidth/maxCount;
        addItemView();
    }

    public ArrayList<TabButtonItem> getTabList() {
        return mTabList;
    }

    public void setTabList(ArrayList<TabButtonItem> tabList) {
        this.mTabList = tabList;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void addItemView() {
        for (int i = 0; i < mTabList.size(); i++) {
            View view = getChildAt(i);
            if(view!=null && view.getTag().toString().equals(i+"")){
                continue;
            }
            TabButtonItem item = mTabList.get(i);


            TabTextView tv = new TabTextView(mContext);
            tv.setText(item.title);
            tv.setSelectColor(mTextPressColor);
            tv.setNormalColor(mTextNormalColor);
            tv.type = item.itemType;
            //tv.setNormalColor(mTextNormalColor);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            tv.setTextColor(mTextNormalColor);
            tv.setGravity(Gravity.CENTER);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    mItemWidth,LayoutParams.MATCH_PARENT);
            lp.gravity = Gravity.CENTER;
            tv.setLayoutParams(lp);
            tv.setTag(i);

            tv.setOnClickListener(this);
            addView(tv,i);

            mChildViews.add(tv);


        }

        if(KlineKind.KIND_T.equals("kind_t_15m")){
            changeIndexSelect(0,true);
        }else if(KlineKind.KIND_T.equals("kind_t_1h")){
            changeIndexSelect(1,true);
        }else if(KlineKind.KIND_T.equals("kind_t_4h")){
            changeIndexSelect(2,true);
        }else if(KlineKind.KIND_T.equals("kind_t_1d")){
            changeIndexSelect(3,true);
        }else if(KlineKind.KIND_T.startsWith("kind_t_")){
            changeIndexSelect(4,true);
            changeTextByIndex(4,KlineKind.kind_t_map.get(KlineKind.KIND_T));
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    public void onClick(View v) {
        int i = (Integer) v.getTag();
        changeSegment(i);
    }

    /**
     *
     * @param position
     */
    public void changeSegment(int position) {
        if(mChildViews==null || mChildViews.size()==0){
            return;
        }
        TabButtonItem item0 = mTabList.get(position);
        if(item0.itemType==2){
            mChildViews.get(position).setTextColor(Color.WHITE);
            if(mCheckTabListener!=null){
                mCheckTabListener.changeCheckItem(position,item0);
            }
            return;
        }

        for (int i = 0; i < mTabList.size(); i++) {
            if (i == position) {
                mChildViews.get(i).setSelected(true);
                mChildViews.get(i).setTextColor(mTextPressColor);
            } else {
                mChildViews.get(i).setSelected(false);
                mChildViews.get(i).setTextColor(mTextNormalColor);
            }
        }

        if(mCheckTabListener!=null){
            mCheckTabListener.changeCheckItem(position,item0);
        }
    }

    public void changeTextByIndex(int index,String title){
        mChildViews.get(index).setText(title);
    }
    public void setCheckTabListener(CheckTabListener mCheckTabListener) {
        this.mCheckTabListener = mCheckTabListener;
    }

    public void changeIndexSelect(int index, boolean b) {
        for (int i = 0; i < mTabList.size(); i++) {
            //TabButtonItem item = mTabList.get(i);
            if (i == index) {
                mChildViews.get(i).setSelected(true);
                mChildViews.get(i).setTextColor(mTextPressColor);
            } else {
                mChildViews.get(i).setSelected(false);
                mChildViews.get(i).setTextColor(mTextNormalColor);
            }
        }
    }

    public boolean selectIsLastIndex(){
        int position = -1;
        for (int i = 0; i < mChildViews.size(); i++) {
            if( mChildViews.get(i).isSelected()){
                position = i;
                break;
            }
        }

        return (position==(mChildViews.size()-1))?true:false;
    }

    public void setMoreDefaultStyle(String title){
        TabTextView tabTextView = mChildViews.get(4);
        tabTextView.setText(title);
        tabTextView.setTextColor(Color.parseColor("#919598"));
    }

    /*public void addTabItemIcon(TabTextView tabTextView){
        Drawable drawable = ContextCompat.getDrawable(mContext,R.mipmap.line_selected2);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        tabTextView.setCompoundDrawables(null,null,drawable,null);
        tabTextView.setCompoundDrawablePadding(PixelUtils.dp2px(mContext,3));
    }*/

    public interface  CheckTabListener{
        public void changeCheckItem(int postion,TabButtonItem item);
    }
}
